<?php
/*
 * This file is part of the oGUI package.
 * (c) 2010 Martin Vancura, DiS. <vancura@martinvancura.cz>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
*/

/**
 * OgElement.
 * Class presents each html elements in oGUI framework
 *
 * @package    oGUI
 * @subpackage core
 * @author     Martin Vancura, DiS. <vancura@martinvancura.cz>
 */
class OgElement implements OgElementInterface {
  
  private $tagName;
  private $attributes;
  private $innerHtml;

  private $classes;
  private $elementId;

  public function  __construct($_tagName, $_innerHtml = null) {
    $this->tagName = $_tagName;
    if($_innerHtml) $this->addComponent($_innerHtml);
  }

  /**
   * Gets array of inner html elements for this instance
   *
   * @author Martin Vancura, DiS. <vancura@martinvancura.cz>
   * @access public
   * @return Array
   */
  public function getInnerHtml() {
    return $this->innerHtml;
  }

  /**
   * Method add component or string into this element inner html
   *
   * @param String $innerHtml
   * @author Martin Vancura, DiS. <vancura@martinvancura.cz>
   * @access public
   * @return Array
   */
  public function setInnerHtml($innerHtml) {
    $this->innerHtml[] = $innerHtml;
  }

  /**
   * Gets Name of this element
   *
   * The name <b>h1</b> presents html tag <h1>h1 heading</h1>
   *
   * @author Martin Vancura, DiS. <vancura@martinvancura.cz>
   * @access public
   * @return String
   */
  public function getTagName() {
    return $this->tagName;
  }

  /**
   * Method adds attribute for this element
   *
   * <ul>
   * <li>You can add only one id attribute. If you add more than one attribute
   * id, previous id will be overwritten.</li>
   * <li>More class attributes can be added. All classes will be separate
   * by space and rendered.</li>
   * <li>You can add any other attribute. If you add same attribute twice will
   * be overwritten.</li>
   * </ul>
   *
   * @param String $name
   * @param String $value
   * @author Martin Vancura, DiS. <vancura@martinvancura.cz>
   * @access public
   * @return String
   */
  public function addAttribute($name, $value) {
    if($name == 'class') {
      $this->classes[] = $value;
    }
    elseif($name == 'id') {
      $this->elementId = $value;
    }
    else {
      $this->attributes[$name] = $value;
    }
  }

  /**
   * Add component to inner html of this instance
   *
   * <ul><li>You can add any number of OgElements or String</li></ul>
   *
   * @param OgElement $component
   */
  public function addComponent($component) {
    $this->innerHtml[] = $component;
  }

  /**
   * Add pointer on this element to pointer holder.
   *
   * @param String $name
   * @param OgElement $component
   * @author Martin Vancura, DiS. <vancura@martinvancura.cz>
   * @access public
   * @see OgPointers
   * @return String
   */
  public function addGettableComponent($name, $component) {
    $this->innerHtml[] = $component;
    $oP = OgPointers::singleton()->addPointer($name, $component);
  }

  /**
   * Method merge all attributes to Array
   * 
   * @return Array 
   */
  public function mergeAttributes() {
    $attrMerge = $this->attributes;
    if($this->classes) $attrMerge['class'] = implode(' ', $this->classes);
    if($this->elementId) $attrMerge['id'] = $this->elementId;
    return $attrMerge;
  }

}
?>
